<?php
class ProgramasController extends ProgramasAppController {

	var $name = 'Programas';
	var $helpers = array('Html', 'Form');
	var $components =  array('Images.Attachment','RequestHandler');
	var $uses = array('Programas.Programa','Images.Image');

	function beforeFilter(){
		parent::beforeFilter();
		$this->layout = 'administrador';
		
	}
	
	function index() {
		$this->Programa->recursive = 0;
		$this->set('programas', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Programa.', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->set('programa', $this->Programa->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$dias ='';
			if(!empty($this->data['Programa']['dias'])){
				foreach($this->data['Programa']['dias'] as $dia){
					$dias .= $dia." ";
				}
				$this->data['Programa']['dias'] = $dias;
			}
			$this->data['Programa']['dias'] = $dias;
			$this->Programa->create();
			if ($this->Programa->save($this->data)) {
				$file_path = $this->Attachment->upload($this->data['Image']['Attachment'],'Programa');
				if($images = $this->Image->save_image('Programa',$this->Programa->getLastInsertId(),$file_path)){
					$this->Session->setFlash(__('Programa guardado', true));
				}else{
					$this->Session->setFlash(__('Error al guardar la imagen. Intente nuevamente', true));
				}
				$this->Session->setFlash(__('The Programa has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Programa could not be saved. Please, try again.', true));
			}
		}
		$dias = array(1=>'Lunes',2=>'Martes',3=>'Miercoles',4=>'Jueves',5=>'Viernes',6=>'Sabado',7=>'Domingo');
		$this->set('dias',$dias);
		
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Programa', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
			$dias ='';
			if(!empty($this->data['Programa']['dias'])){
				foreach($this->data['Programa']['dias'] as $dia){
					$dias .= $dia." ";
				}
				$this->data['Programa']['dias'] = $dias;
			}
			if ($this->Programa->save($this->data)) {
				$this->Session->setFlash(__('The Programa has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Programa could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Programa->read(null, $id);
			$this->data['Programa']['dias'] = explode(" ",$this->data['Programa']['dias']);
		}
		$dias = array(1=>'Lunes',2=>'Martes',3=>'Miercoles',4=>'Jueves',5=>'Viernes',6=>'Sabado',7=>'Domingo');
		$this->set('dias',$dias);
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Programa', true));
			$this->redirect(array('action'=>'index'));
		}
		$new = $this->Programa->findById($id);
		if ($this->Programa->del($id)){
			$this->Image->delete_img('Programa',$id);
			$this->Attachment->delete_files($new['Image']['files'],'Programa');
			$this->redirect($this->referer());
		}
	}

}
?>